//地址编辑
<template>
<div>
     <div class="user-header">
            <span class="iconfont text" @click="back">&#xe601;</span>
            <span class="text2">{{this.$route.query.type=='1'?"新增地址":"编辑地址"}}</span>
            <span class="iconfont text">&#xe62b;</span>
    </div>
    <van-address-edit
    :area-list="areaList"
    show-postal:true
    show-delete
    show-set-default
    show-search-result
    :search-result="searchResult"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
    @change-detail="onChangeDetail"
    :address-info="AddressInfo"
  />
</div>

</template>
<script>
import { Toast } from 'vant'
import {addAddress, DeleteAddress, EditAddress} from '@/api/api'
import { areaList } from '@vant/area-data'
import PubSub from 'pubsub-js'
export default {
  data () {
    return {
      searchResult: [],
      areaList,
      AddressInfo: {},
      addressId: null
    }
  },
  beforeCreate () {
    PubSub.subscribe('item', (msg, data) => {
      // console.log(data, '?????')
      this.AddressInfo = {
        id: data.Id,
        name: data.name,
        tel: data.tel,
        isDefault: !!data.defaultFlag
      }
      this.addressId = data.id
    })
  },
  methods: {
    // 返回上一级
    back () {
      this.$router.push('/Ress')
    },
    onSave (content) {
      Toast('save')
      const params = {
        userName: content.name,
        userPhone: content.tel,
        provinceName: content.province,
        cityName: content.city,
        regionName: content.county,
        detailAddress: content.addressDetail,
        defaultFlag: content.isDefault ? 1 : 0
      }
      if (this.$route.query.type !== '1') {
        console.log('123')
        params['addressId'] = this.addressId
      }
      this.$route.query.type === '1'
        ? addAddress(params).then((res) => {
          console.log(res)
          if (res.data.message === 'SUCCESS') {
            this.$router.push('/Ress')
          } else {
            console.log(res.data.message)
          }
        }) : EditAddress(params).then((res) => {
          console.log(params)
          if (res.data.message === 'SUCCESS') {
            this.$router.push('/Ress')
          } else {
            console.log(res.data.message)
          }
        })
    },
    onDelete () {
      Toast('delete')
      console.log(this.addressId)
      DeleteAddress(this.addressId).then((res) => {
        if (res.data.message === 'SUCCESS') {
          this.$router.push('/Ress')
        } else {
          console.log(res.data.message)
        }
      })
    },
    onChangeDetail (val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区'
          }
        ]
      } else {
        this.searchResult = []
      }
    }
  }
}
</script>

<style lang="less" scoped>
.user-header{
        width: 100%;
        height: 1rem;
        border-bottom: 1px solid #F7F8F8;
        line-height: 1.2rem;
        display: flex;
        text-align: center;
        .text{
            width: 10%;
        }
        .text2{
            width: 80%;
        }
    }
</style>
